package com.springblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springblog.common.utils.R;
import com.springblog.entity.DeviceEntity;
import com.springblog.entity.UserEntity;
import com.springblog.service.DeviceService;
import com.springblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用于MongDB操作
 *
 * @author ifredom@gmail.com
 * @since 2022-04-23
 */
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private UserService userService;

    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        System.out.println("========: " + params);
        // 根据设备类型查找设备
        Query query = new Query(Criteria.where("deviceType").is(params.get("deviceType")));

        PageRequest pageable = PageRequest.of(1, 10);

        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        query.with(pageable);
        query.with(sort);

        List<DeviceEntity> deviceEntityList = mongoTemplate.find(query, DeviceEntity.class);

        return R.ok().put("list", deviceEntityList);
    }

    // 查询该设备得拥有者。 mongodb + mysql查询
    @GetMapping("/owner")
    public R select(@RequestBody DeviceEntity deviceEntity) {

        // 根据设备类型查找设备
        DeviceEntity oneDeviceEntity = mongoTemplate.findOne(new Query(Criteria.where("deviceType").is(deviceEntity.getDeviceType())), DeviceEntity.class);

        QueryWrapper<UserEntity> wrapper = new QueryWrapper<UserEntity>();
        if (oneDeviceEntity != null) {
            wrapper.eq("device_id", oneDeviceEntity.getDeviceId());
        }
        // 根据设备类型，查找拥有者
        UserEntity userEntity = userService.getOne(wrapper);
        return R.ok().put("user", userEntity);
    }

    @PostMapping("/save")
    public R save(@RequestBody DeviceEntity deviceEntity) {
        UserEntity userEntity = userService.getById(1L);
        userEntity.setDeviceId("0000001");

        userService.updateById(userEntity);
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody DeviceEntity deviceEntity) {
        UserEntity userEntity = userService.getById(1L);
        userEntity.setDeviceId("0000001");

        userService.updateById(userEntity);
        return R.ok();
    }

    // 聚合查询
    @PostMapping("/update2")
    public R aggregation(@RequestBody DeviceEntity deviceEntity) {


//        //查询条件
//        Criteria criteria = new Criteria("deviceId").in(deviceIdList);
//
//        Aggregation aggregation = Aggregation.newAggregation(
//                match(criteria),
//                unwind("logs"),
//                addFields().addField("yue").withValueOfExpression("{ $dateToString : { format :'%Y-%m', date: '$logs.createTime',timezone: 'Asia/Shanghai' }}").build(),
//                group("yue", "logs.type").count().as("count")
//        );

        return R.ok();
    }

}
